home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / prio / _m_heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  2.7 KB  |  131 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _m_heap.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/impl/m_heap.h>
  16.  
  17. m_heap::m_heap(int m)
  18. { if (m<=0) error_handler(1,string("m_heap constructor: illegal size = %d",m));
  19.   count = 0;
  20.   start = 0;
  21.   offset = 0;
  22.   M = m+1;
  23.   T = new list_item[M+1];
  24.   for(int i=0; i<=M; i++) T[i] = L.append(GenPtr(MAXINT));
  25. }
  26.  
  27. void m_heap::clear()
  28. { L.clear();
  29.   for(int i=0; i<=M; i++) T[i] = L.append(GenPtr(MAXINT));
  30.   count = 0;
  31.   start = 0;
  32.   offset = 0;
  33.  
  34. m_heap_item m_heap::first_item() const
  35. { list_item it = L.first();
  36.   while (it && L.inf(it) == GenPtr(MAXINT)) it = L.succ(it);
  37.   return it;
  38.  }
  39.   
  40. m_heap_item m_heap::next_item(m_heap_item it) const
  41. { it = L.succ(it);
  42.   while (it && L.inf(it) == GenPtr(MAXINT)) it = L.succ(it);
  43.   return it;
  44.  }
  45.  
  46. m_heap_item m_heap::insert(GenPtr kk, GenPtr info)   // int key
  47. { int key = int(kk);
  48.   int k;
  49.   if (count)
  50.    { find_min();
  51.      k = key-offset; 
  52.     }
  53.   else 
  54.    { start = key%M;
  55.      k = 0;
  56.      offset = key;
  57.     }
  58.  
  59.   if (k<0 || k>=M) 
  60.    error_handler(1,string("m_heap insert: illegal key %d \n",key));
  61.  
  62.   copy_inf(info);
  63.  
  64.   count++;
  65.   return L.insert(info,T[(start+k)%M]); 
  66. }
  67.  
  68. m_heap_item m_heap::find_min() const
  69. { if (count)
  70.   { while (L.succ(T[start])==T[start+1])
  71.     { (*(int*)&offset)++;
  72.       (*(int*)&start)++;
  73.       if (start == M) (*(int*)&start) = 0;
  74.      }
  75.     return L.succ(T[start]);
  76.    }
  77.  else return 0;
  78. }
  79.  
  80. GenPtr m_heap::del_min()
  81. { if (count)
  82.    { find_min();
  83.      count--;
  84.      m_heap_item p = L.succ(T[start]);
  85.      GenPtr inf = L.contents(p);
  86.      L.del(p);
  87.      return inf;
  88.     }
  89.   else error_handler(1,"m_heap del_min: heap is empty");
  90.   return 0;
  91. }
  92.  
  93. void m_heap::del_item(m_heap_item it) 
  94. { GenPtr x = L.contents(it);
  95.   clear_inf(x);
  96.   L.del(it); 
  97.   count--;
  98. }
  99.  
  100. void m_heap::change_key(m_heap_item it, GenPtr kk)  // int key
  101. { int key = int(kk);
  102.   find_min();
  103.   GenPtr inf = L.contents(it);
  104.   L.del(it); 
  105.   int k = key - offset;
  106.   if (k<0 || k>=M) 
  107.    error_handler(1,string("m_heap change key: illegal key %d\n",key));
  108.   L.insert(inf,T[(start+k)%M]);
  109. }
  110.  
  111.  
  112. void m_heap::print() const
  113. { m_heap_item p;
  114.   cout << string("count = %d   start = %d   offset = %d\n",count,start,offset);
  115.   for (int i=0;i<M;i++) 
  116.   if (L.contents(L.succ(T[i])) != GenPtr(MAXINT))
  117.   { cout << string("%3d: ",i);
  118.     p = L.succ(T[i]);
  119.     while (L.contents(p) != GenPtr(MAXINT))
  120.      { cout << string("(%d) ",L.contents(p));
  121.        p = L.succ(p);
  122.       }
  123.    cout << "\n";
  124.    }
  125.    cout << "\n";
  126. }
  127.  
  128.  
  129.